package smartcase.config;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import smartcase.base.ApiStandard;
import smartcase.base.log.Log;
import smartcase.base.log.LogFactory;

import static smartcase.base.StandardKt.error;

@ControllerAdvice
public class GlobalThrowableCaptors {

  private final Log log = LogFactory.getLog(GlobalThrowableCaptors.class);

  @ExceptionHandler(value = Throwable.class)
  @ResponseBody
  @ResponseStatus(code = HttpStatus.OK)
  public Object handlerServiceException(Throwable e) {
    log.error(e.getMessage());
    e.printStackTrace();
    return error(ApiStandard.INVALID_REQUEST, e.getMessage());
  }
}
